package chapter17;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import chapter15.MessagePanel;

public class SliderDemo extends JFrame{
	
	private JSlider jsldHort = new JSlider(JSlider.HORIZONTAL);
	private JSlider jsldVert = new JSlider(JSlider.VERTICAL);
	private MessagePanel messagePanel = new MessagePanel("Welcome to java");

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SliderDemo frame = new SliderDemo();
		frame.setTitle("SliderDemo");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public SliderDemo(){
		setLayout(new BorderLayout(5, 5));
		add(messagePanel, BorderLayout.CENTER);
		add(jsldHort, BorderLayout.SOUTH);
		add(jsldVert, BorderLayout.EAST);
		
		jsldHort.setMaximum(50);
		jsldHort.setPaintLabels(true);//
		jsldHort.setPaintTicks(true);
		jsldHort.setMajorTickSpacing(10);
		jsldHort.setMinorTickSpacing(1);
		jsldHort.setPaintTrack(false);
		
		jsldVert.setInverted(true);
		jsldVert.setMaximum(10);
		jsldVert.setPaintLabels(true);
		jsldVert.setPaintTicks(true);
		jsldVert.setMajorTickSpacing(10);
		jsldVert.setMinorTickSpacing(1);
		
		jsldHort.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				double value = jsldHort.getValue();
				double maximumValue = jsldHort.getMaximum();
				double newX = value * messagePanel.getWidth() / maximumValue;
				messagePanel.setxCoordiante((int) newX);
			}
		});
		
		jsldVert.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				double value = jsldVert.getValue();
				double maximumValue = jsldVert.getMaximum();
				double newY = value * messagePanel.getHeight() / maximumValue;
				messagePanel.setyCoordinate((int) newY);
			}
		});
	}

}
